﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BLL;

public partial class Admin_ManageBZ : System.Web.UI.Page
{
    BLL.BLLBZ bz = new BLL.BLLBZ();

    string errMsg = "";
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GridBZ.DataSource = bz.RetAllBZ(out errMsg);
            GridBZ.DataBind();
        }
    }
    protected void btnSure_Click(object sender, EventArgs e)
    {
        string SBzId = this.tbBzId.Text.Trim();//this.tbRootName.Text.Trim();
        int BzId = int.Parse(SBzId);
        string BzMs = this.tbBzMs.Text.Trim();
        string sobzid = this.lbBzId.Text;
        string message = "";
        this.lbBzId.Text = "";
        this.tbBzId.Text = "";
        this.tbBzMs.Text = "";
        try
        {
            if (sobzid == "")
            {
                if (bz.SaveBZ(BzId, BzMs, out errMsg))
                {
                    GridBZ.DataSource = bz.RetAllBZ(out errMsg);
                    GridBZ.DataBind();
                    message = "添加班组成功！";
                }
                else
                {
                    message = "添加班组失败！请检查是否存在相同的班组编号！";
                }
            }
            else
            {
                int obzid = int.Parse(sobzid); 
                if (bz.UpDateBZ(obzid, BzId, BzMs, out errMsg))
                {
                    GridBZ.DataSource = bz.RetAllBZ(out errMsg);
                    GridBZ.DataBind();
                    message = "修改班组成功";
                }
                else
                {
                    message = "修改班组失败！请检查是否存在相同的班组编号！";
                }
            }
        }
        catch (Exception ex)
        {
            message = "添加班组失败！失败信息：" + ex.Message.ToString();
        }

        Response.Write("<script>alert('" + message + "')</script>");

    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        this.tbBzId.Text = "";
        this.tbBzMs.Text = "";
        this.lbBzId.Text = "";
        this.lbBzMs.Text = "";
    }

    protected void gridBZ_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        this.tbBzId.Text = GridBZ.DataKeys[e.NewSelectedIndex].Value.ToString();
        this.tbBzMs.Text = GridBZ.Rows[e.NewSelectedIndex].Cells[1].Text;
        lbBzId.Text = this.tbBzId.Text;
        lbBzMs.Text = this.tbBzMs.Text;
    }

    protected void gridBZ_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string SBzId = GridBZ.DataKeys[e.RowIndex].Value.ToString();
        int BzId = int.Parse(SBzId);
        string message = "";
        if (bz.DeleteBZ(BzId,out errMsg))
        {
            GridBZ.DataSource = bz.RetAllBZ(out errMsg);
            GridBZ.DataBind();
            message = "删除角色名成功";
        }
        else
        {
            message = "修改角色名失败！请检查是否存在相同的角色名！";
        }
        Response.Write("<script>alert('" + message + "')</script>");
    }
}
